

Component({
  properties: {
    tree: {
      type: Array,
      value: [],
      observer(newVal, oldVal, changedPath) {
        if (newVal && newVal.length > 0) {
          let region = []
          let list = []
          let index = []
          region[0] = newVal[0].text
          region[1] = newVal[0].children[0].text
          list[0] = newVal.map(x => x.text)
          list[1] = newVal[0].children.map(x => x.text)
          index[0] = 0
          index[1] = 0
          if (newVal[0].children[0].children && newVal[0].children[0].children.length > 0) {
            region[2] = newVal[0].children[0].children[0].text
            list[2] = newVal[0].children[0].children.map(x => x.text)
            index[2] = 0
          } else {
            list[2] = ['']
            index[2] = -1
          }
          this.setData({
            region,
            treeList: list,
            regionIndex: index
          })
        }
      }
    },
    index: {
      type: Array,
      value: [],
      observer(newVal, oldVal, changedPath) {
        if (newVal && newVal.length > 0) {
          this.setData({
            regionIndex: newVal
          })
        }
      }
    },
    city: {
      type: Object,
      value: {},
      observer(newVal, oldVal, changedPath) {
        if (newVal && Object.keys(newVal).length > 0) {
          let city = [newVal.province, newVal.city]
          if (newVal.area && newVal.area.length > 0) {
            city[2] = newVal.area
          } else {
            city[2] = ''
          }
          this.setData({
            region: city
          })
        }
      }
    }
  },
  data: {
    // 初始值
    // region: ['广东省', '广州市', '天河区'],
    region: [],
    regionIndex: [],
    treeList: [[], [], []]
  },
  methods: {
    bindRegionChange: function (e) {
      const value = e.detail.value
      // console.log(value)
      const tree = this.properties.tree
      this.setData({
        region: [
          tree[value[0]].text,
          tree[value[0]].children[value[1]].text,
          tree[value[0]].children[value[1]].children && tree[value[0]].children[value[1]].children.length > 0
            ? tree[value[0]].children[value[1]].children[value[2]].text: ''
        ]
      })
      this.triggerEvent('saveCity', { city: value })
    },

    bindColumnChange(e) {
      const { column, value } = e.detail
      let index = this.data.regionIndex
      const list = this.data.treeList
      const tree = this.properties.tree
      if (column === 0) {
        list[1] = tree[value].children.map(x => x.text)
        index[0] = value
        index[1] = 0
        if (tree[value].children[0] && tree[value].children[0].children && tree[value].children[0].children.length > 0) {
          list[2] = tree[value].children[0].children.map(x => x.text)
          index[2] = 0
        } else {
          list[2] = []
          index[2] = -1
        }
      } else if (column === 1) {
        if (tree[index[0]].children[value] && tree[index[0]].children[value].children && tree[index[0]].children[value].children.length > 0) {
          list[2] = tree[index[0]].children[value].children.map(x => x.text)
          index[2] = 0
        } else {
          list[2] = []
          index[2] = -1
        }
      }
      this.setData({
        treeList: list,
        regionIndex: index
      })
    }
  }
});
